import argparse
import os
from collections import defaultdict
from prettytable import PrettyTable


def print_dict(d):
    x = PrettyTable()
    x.field_names = ["Function Name", "Counts"]

    for key in d:
        x.add_row([key, d[key]])
    print(x)


def run(args):
    path = args.path
    if not os.path.exists(path):
        raise Exception("Dir Not Found!")

    if not os.path.isdir(path):
        raise Exception("Not A Dir!")

    file_list = os.listdir(path)

    auto_dict = defaultdict(lambda: 0)
    normal_dict = defaultdict(lambda: 0)
    is_auto = False

    for filename in file_list:
        split_names = filename.split("-")
        if len(split_names) < 2:
            continue

        is_auto = split_names[0] == "auto_schedule"

        if is_auto:
            auto_dict[split_names[1]] += 1
        else:
            normal_dict[split_names[0]] += 1

    print_dict(auto_dict)
    print("----------------------------------------")
    print_dict(normal_dict)


if __name__ == "__main__":
    parser = argparse.ArgumentParser("Statistic Util")
    parser.add_argument("-p", "--path", type=str, help="The path of .so data")
    args = parser.parse_args()
    run(args)
